package com.lrd.snapshotorder.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lrd.snapshotorder.entity.OrderTask;
import com.lrd.snapshotorder.entity.SnapshotOrder;
import com.lrd.snapshotorder.protocol.dto.order.SnapshotOrderQueryDto;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Objects;

public interface SnapshotOrderMapper extends BaseMapper<SnapshotOrder> {

    default Page<SnapshotOrder> snapshotOrderPageList(SnapshotOrderQueryDto queryDto) {
        return this.selectPage(
                new Page<>(queryDto.getPageIndex(), queryDto.getPageSize()),
                Wrappers.lambdaQuery(SnapshotOrder.class)
                        .eq(Objects.nonNull(queryDto.getBatchNumber()), SnapshotOrder::getBatchNumber, queryDto.getBatchNumber())
                        .ge(Objects.nonNull(queryDto.getPostTimeBegin()), SnapshotOrder::getPostTime, queryDto.getPostTimeBegin())
                        .le(Objects.nonNull(queryDto.getPostTimeEnd()), SnapshotOrder::getPostTime, queryDto.getPostTimeEnd())
        );
    }

    void insertBatch(@Param("batch") List<SnapshotOrder> batch);

    default List<SnapshotOrder> snapshotOrderAllList(SnapshotOrderQueryDto queryDto) {
        return this.selectList(
                Wrappers.lambdaQuery(SnapshotOrder.class)
                        .eq(Objects.nonNull(queryDto.getBatchNumber()), SnapshotOrder::getBatchNumber, queryDto.getBatchNumber())
                        .ge(Objects.nonNull(queryDto.getPostTimeBegin()), SnapshotOrder::getPostTime, queryDto.getPostTimeBegin())
                        .le(Objects.nonNull(queryDto.getPostTimeEnd()), SnapshotOrder::getPostTime, queryDto.getPostTimeEnd())
        );
    }

    default int selectCountByBatchNumber(OrderTask orderTask) {
        return this.selectCount(
                Wrappers.lambdaQuery(SnapshotOrder.class)
                        .eq(SnapshotOrder::getBatchNumber, orderTask.getBatchNumber())
        );
    }
}